package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    /**
     * 通过跟进表中的商机id查询所有的跟进记录
     * @param id 商机id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> selectAllTrackRecord(Long id) {
        List<TbBusinessTrackRecord> list = tbBusinessTrackRecordMapper.selectAllTrackRecord(id);
        return list;
    }

    /**
     * 新增商机跟进记录
     * @param businessTrackVo
     */
    @Transactional
    @Override
    public void addTrackRecord(BusinessTrackVo businessTrackVo) {
        //1、首先得到tb_business，并且给他赋值，修改原本商机表中的数据
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness,"id");
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusinessMapper.updateTbBusiness(tbBusiness);
        //2、还需要给 跟进表中的数据进行赋值 没有creat_by 需要手动赋值
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
        String username = SecurityUtils.getUsername();
        tbBusinessTrackRecord.setCreateBy(username);
        tbBusinessTrackRecordMapper.addTrackRecord(tbBusinessTrackRecord);
    }
}
